<template>
  <Dropdown placement="bottomLeft" :overlayClassName="`${prefixCls}-dropdown-overlay`">
    <span :class="[prefixCls, `${prefixCls}--${theme}`]" class="flex">
      <img :class="`${prefixCls}__header`" :src="getUserInfo.avatar" />
      <span :class="`${prefixCls}__info hidden md:block`">
        <span :class="`${prefixCls}__name`" class="truncate">
          {{ getUserInfo.account }}
        </span>
      </span>
    </span>

    <template #overlay>
      <Menu @click="handleMenuClick">
        <MenuItem v-if="getUseLockPage" key="password" text="修改密码" icon="ion:KeyOutlined" />
        <MenuItem v-if="getUseLockPage" key="lock" :text="t('layout.header.tooltipLock')"
          icon="ion:lock-closed-outline" />
        <MenuItem key="logout" :text="t('layout.header.dropdownItemLoginOut')" icon="ion:power-outline" />
        <!-- <MenuItem key="ver" text="12.18.0" icon="ion:info-circle-outlined">
        </MenuItem> -->
     
      </Menu>

    </template>
  </Dropdown>
  <LockAction @register="register" />
  <passWordAction @register="registerPassWord" />
</template>
<script lang="ts" setup>
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { Dropdown, Menu } from 'ant-design-vue';
import type { MenuInfo } from 'ant-design-vue/lib/menu/src/interface';
import { computed } from 'vue';
import { DOC_URL } from '@/settings/siteSetting';
import { useUserStore } from '@/store/modules/user';
import { useHeaderSetting } from '@/hooks/setting/useHeaderSetting';
import { useI18n } from '@/hooks/web/useI18n';
import { useDesign } from '@/hooks/web/useDesign';
import { useModal } from '@/components/Modal';
import headerImg from '@/assets/images/header.jpg';
import { propTypes } from '@/utils/propTypes';
import { openWindow } from '@/utils';
import { createAsyncComponent } from '@/utils/factory/createAsyncComponent';

type MenuEvent = 'logout' | 'doc' | 'lock' | 'api';
const windowInstanse: any = window;
const MenuItem = createAsyncComponent(() => import('./DropMenuItem.vue'));
const LockAction = createAsyncComponent(() => import('../lock/LockModal.vue'));
const passWordAction = createAsyncComponent(() => import('../password/editModal.vue'));

defineOptions({ name: 'UserDropdown' });

defineProps({
  theme: propTypes.oneOf(['dark', 'light']),
});

const { prefixCls } = useDesign('header-user-dropdown');
const { t } = useI18n();
const { getShowDoc, getUseLockPage, getShowApi } = useHeaderSetting();
const userStore = useUserStore();

const getUserInfo = computed(() => {
  let { realName = '', avatarUrl, desc, account } = userStore.getUserInfo || {};
  avatarUrl = avatarUrl ? windowInstanse.config.base_url + avatarUrl : '';
  return { realName, avatar: avatarUrl || headerImg, desc, account };
});

const [register, { openModal }] = useModal();
const [registerPassWord, { openModal: openPassWordModal }] = useModal();
const [registerApi, { openModal: openApiModal }] = useModal();

function handleLock() {
  openModal(true);
}
function openPassWord() {
  openPassWordModal(true);
}

function handleApi() {
  openApiModal(true, {});
}

//  login out
function handleLoginOut() {
  userStore.confirmLoginOut();
}

// open doc
function openDoc() {
  openWindow(DOC_URL);
}

function handleMenuClick(e: MenuInfo) {
  switch (e.key as MenuEvent) {
    case 'logout':
      handleLoginOut();
      break;
    case 'password':
      openPassWord();
      break;
    case 'lock':
      handleLock();
      break;
    case 'api':
      handleApi();
      break;
  }
}
</script>
<style lang="less">
@prefix-cls: ~'@{namespace}-header-user-dropdown';

.@{prefix-cls} {
  align-items: center;
  height: @header-height;
  padding: 0 0 0 10px;
  padding-right: 10px;
  overflow: hidden;
  font-size: 12px;
  cursor: pointer;

  img {
    width: 24px;
    height: 24px;
    margin-right: 12px;
  }

  &__header {
    border-radius: 50%;
  }

  &__name {
    font-size: 14px;
  }

  &--dark {
    &:hover {
      background-color: @header-dark-bg-hover-color;
    }
  }

  &--light {
    &:hover {
      background-color: @header-light-bg-hover-color;
    }

    .@{prefix-cls}__name {
      color: @text-color-base;
    }

    .@{prefix-cls}__desc {
      color: @header-light-desc-color;
    }
  }

  &-dropdown-overlay {
    .ant-dropdown-menu-item {
      min-width: 160px;
    }
  }
}
</style>
